package old_question_new_working;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * Author: 笨蛋超人不会飞
 * Date:  16:43
 */
public class q_56 {
}
class Solution_56 {
    public int[][] merge(int[][] intervals) {
//        先处理特殊的情况
        if(intervals==null||intervals.length<=1){
            return intervals;
        }
//        先创建一个ArrayList数组来存储中间过程产生的值
        List<ArrayList<Integer>> temp=new ArrayList<>();
//        先将原本的数组进行排序
        maopao_sort(intervals);
        int arr[]=new int[2];

        int i=0;
        for ( i = 0; i < intervals.length-1; i++) {
//            如果相同，那么先合并,可以将合并的结果先给后面的那一个
            if(intervals[i][0]==intervals[i+1][0]){
                intervals[i+1][1]=intervals[i+1][1]>intervals[i][1]?intervals[i+1][1]:intervals[i][1];
            }else{
//                先看看是否可以直接添加进来
                if(intervals[i][1]<intervals[i+1][0]){
                    ArrayList<Integer> list=new ArrayList<>();
                    list.add(intervals[i][0]);
                    list.add(intervals[i][1]);
                    temp.add(list);
                }else {
                    intervals[i+1][0]=intervals[i][0];
                    intervals[i+1][1]=intervals[i+1][1]>intervals[i][1]?intervals[i+1][1]:intervals[i][1];
                }
            }

        }
        ArrayList<Integer> list=new ArrayList<>();
        list.add(intervals[i][0]);
        list.add(intervals[i][1]);
        temp.add(list);
        int[][] result = convertListToArray(temp);
        return result;

    }

    public void maopao_sort(int[][] intervals){
        if(intervals==null||intervals.length==0) {
            return ;
        }
        int len=intervals.length;
        for (int i = 0; i < len-1; i++) {
            for (int j = 0; j < len-i-1; j++) {
                if(intervals[j][0]>intervals[j+1][0]){
                    int temp[]=intervals[j];
                    intervals[j]=intervals[j+1];
                    intervals[j+1]=temp;

                }
            }
        }
    }
    public static int[][] convertListToArray(List<ArrayList<Integer>> list) {
        // 检查列表是否为空
        if (list == null || list.isEmpty()) {
            return new int[0][];
        }

        // 确定数组的行数和列数
        int rows = list.size();
        int cols = 0;
        for (ArrayList<Integer> row : list) {
            cols = Math.max(cols, row.size());
        }

        // 创建二维数组
        int[][] array = new int[rows][cols];

        // 遍历列表并将元素复制到数组中
        for (int i = 0; i < rows; i++) {
            ArrayList<Integer> row = list.get(i);
            for (int j = 0; j < row.size(); j++) {
                array[i][j] = row.get(j);
            }
        }

        return array;
    }

//    官方题解

    public int[][] merge_02(int[][] intervals) {
        if (intervals.length == 0) {
            return new int[0][2];
        }
        Arrays.sort(intervals, new Comparator<int[]>() {
            public int compare(int[] interval1, int[] interval2) {
                return interval1[0] - interval2[0];
            }
        });
        List<int[]> merged = new ArrayList<int[]>();
        for (int i = 0; i < intervals.length; ++i) {
            int L = intervals[i][0], R = intervals[i][1];
            if (merged.size() == 0 || merged.get(merged.size() - 1)[1] < L) {
                merged.add(new int[]{L, R});
            } else {
                merged.get(merged.size() - 1)[1] = Math.max(merged.get(merged.size() - 1)[1], R);
            }
        }
        return merged.toArray(new int[merged.size()][]);
    }


}